home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / admin / snmp-dev.000 / snmp-dev / snmpset.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-09  |  15.5 KB  |  553 lines

  1. /*
  2.  * snmpset.c - send snmp SET requests to a network entity.
  3.  *
  4.  */
  5. /***********************************************************************
  6.     Copyright 1988, 1989, 1991, 1992 by Carnegie Mellon University
  7.  
  8.                       All Rights Reserved
  9.  
  10. Permission to use, copy, modify, and distribute this software and its 
  11. documentation for any purpose and without fee is hereby granted, 
  12. provided that the above copyright notice appear in all copies and that
  13. both that copyright notice and this permission notice appear in 
  14. supporting documentation, and that the name of CMU not be
  15. used in advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.  
  17.  
  18. CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  19. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  20. CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  21. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  22. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  23. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  24. SOFTWARE.
  25. ******************************************************************/
  26. #include <sys/types.h>
  27. #include <netinet/in.h>
  28. #include <stdio.h>
  29. #include <ctype.h>
  30. #include <sys/time.h>
  31. #include <netdb.h>
  32. #ifdef linux
  33. # include <stdlib.h>
  34. # include <string.h>
  35. # include <unistd.h>
  36. # include <arpa/inet.h>
  37. #endif
  38.  
  39. #include "snmp.h"
  40. #include "asn1.h"
  41. #include "mib.h"
  42. #include "snmp_impl.h"
  43. #include "snmp_api.h"
  44. #include "snmp_client.h"
  45. #include "party.h"
  46. #include "context.h"
  47. #include "view.h"
  48. #include "acl.h"
  49.  
  50. extern void snmp_add_var ();
  51.  
  52.  
  53. extern int  errno;
  54. int    snmp_dump_packet = 0;
  55.  
  56. static int ascii_to_binary ();
  57. static int hex_to_binary ();
  58.  
  59.  
  60. void
  61. usage(){
  62. #ifdef SVR4
  63.     fprintf(stderr, "Usage: snmpset -v 1 [-q] hostname community [objectID type value]+    or:\n");
  64.     fprintf(stderr, "Usage: snmpset [-v 2] [-q] hostname noAuth [objectID type value]+     or:\n");
  65.     fprintf(stderr, "Usage: snmpset [-v 2] [-q] hostname srcParty dstParty context [oID type val]+\n");
  66.     fprintf(stderr, "\twhere type is one of: i, s, x, d, n, o, t, a\n");
  67.     fprintf(stderr, "\t\ti: INTEGER, s: STRING, x: HEX STRING, d: DECIMAL STRING\n");
  68.     fprintf(stderr, "\t\tn: NULLOBJ, o: OBJID, t: TIMETICKS, a: IPADDRESS\n");
  69. #else
  70.     fprintf(stderr, "Usage: snmpset -v 1 hostname community [objectID type value]+    or:\n");
  71.     fprintf(stderr, "Usage: snmpset [-v 2] hostname noAuth [objectID type value]+     or:\n");
  72.     fprintf(stderr, "Usage: snmpset [-v 2] hostname srcParty dstParty context [oID type val]+\n");
  73.     fprintf(stderr, "\twhere type is one of: i, s, x, d, n, o, t, a\n");
  74.     fprintf(stderr, "\t\ti: INTEGER, s: STRING, x: HEX STRING, d: DECIMAL STRING\n");
  75.     fprintf(stderr, "\t\tn: NULLOBJ, o: OBJID, t: TIMETICKS, a: IPADDRESS\n");
  76. #endif
  77. }
  78.  
  79. int
  80. main(argc, argv)
  81.     int        argc;
  82.     char    *argv[];
  83. {
  84.     struct snmp_session session, *ss;
  85.     struct snmp_pdu *pdu, *response;
  86.     struct variable_list *vars;
  87.     int    arg;
  88.     char *hostname = NULL;
  89.     char *community = NULL;
  90.     int timeout_flag = 0, timeout = 0, retransmission_flag = 0;
  91.     int retransmission = 0;            /* YYY: check init */
  92.     int    count, current_name = 0, current_type = 0, current_value = 0;
  93.     char *names[128], types[128], *values[128];
  94.     oid name[MAX_NAME_LEN];
  95.     int name_length;
  96.     int status;
  97.     int version = 2;
  98.     int port_flag = 0;
  99.     int dest_port = 0;
  100.     u_long srcclock = 0, dstclock = 0;        /* YYY: check init */
  101.     int clock_flag = 0;
  102.     oid src[MAX_NAME_LEN], dst[MAX_NAME_LEN], context[MAX_NAME_LEN];
  103.     int srclen = 0, dstlen = 0, contextlen = 0;
  104.     struct partyEntry *pp;
  105.     struct contextEntry *cxp;
  106.     int trivialSNMPv2 = FALSE;
  107.     struct hostent *hp;
  108.     u_long destAddr;
  109.  
  110.  
  111.  
  112.     init_mib();
  113.     for(arg = 1; arg < argc; arg++){
  114.     if (argv[arg][0] == '-'){
  115.         switch(argv[arg][1]){
  116.         case 'd':
  117.             snmp_dump_packet++;
  118.             break;
  119. #ifdef SVR4
  120.         case 'q':
  121.             quick_print++;
  122.             break;
  123. #endif
  124.                 case 'p':
  125.                     port_flag++;
  126.                     dest_port = atoi(argv[++arg]);
  127.                     break;
  128.                 case 't':
  129.                     timeout_flag++;
  130.                     timeout = atoi(argv[++arg]) * 1000000L;
  131.                     break;
  132.                 case 'r':
  133.                     retransmission_flag++;
  134.                     retransmission = atoi(argv[++arg]);
  135.                     break;
  136.                 case 'c':
  137.                     clock_flag++;
  138.                     srcclock = atoi(argv[++arg]);
  139.                     dstclock = atoi(argv[++arg]);
  140.                     break;
  141.                 case 'v':
  142.                     version = atoi(argv[++arg]);
  143.                     if (version < 1 || version > 2){
  144.                         fprintf(stderr, "Invalid version\n");
  145.                         usage();
  146.                         exit(1);
  147.                     }
  148.                     break;
  149.         default:
  150.             printf("invalid option: -%c\n", argv[arg][1]);
  151.             break;
  152.         }
  153.         continue;
  154.     }
  155.     if (hostname == NULL){
  156.         hostname = argv[arg];
  157.         } else if (version == 1 && community == NULL){
  158.             community = argv[arg];
  159.     } else if (version == 2 && srclen == 0 && !trivialSNMPv2){
  160.         if (read_party_database("/etc/party.conf") > 0){
  161.         fprintf(stderr,
  162.             "Couldn't read party database from /etc/party.conf\n");
  163.         exit(0);
  164.         }
  165.             if (read_context_database("/etc/context.conf") > 0){
  166.                 fprintf(stderr,
  167.                         "Couldn't read context database from /etc/context.conf\n");
  168.                exit(0);
  169.             }
  170.         if (read_acl_database("/etc/acl.conf") > 0){
  171.         fprintf(stderr,
  172.             "Couldn't read access control database from /etc/acl.conf\n");
  173.         exit(0);
  174.         }
  175.             if (!strcasecmp(argv[arg], "noauth")){
  176.                 trivialSNMPv2 = TRUE;
  177.             } else {
  178.                 party_scanInit();
  179.         for(pp = party_scanNext(); pp; pp = party_scanNext()){
  180.             if (!strcasecmp(pp->partyName, argv[arg])){
  181.             srclen = pp->partyIdentityLen;
  182. #ifdef SVR4
  183.             memmove(src, pp->partyIdentity, srclen * sizeof(oid));
  184. #else
  185.             bcopy(pp->partyIdentity, src, srclen * sizeof(oid));
  186. #endif
  187.             break;
  188.             }
  189.         }
  190.         if (!pp){
  191.             srclen = MAX_NAME_LEN;
  192.             if (!read_objid(argv[arg], src, &srclen)){
  193.             printf("Invalid source party: %s\n", argv[arg]);
  194.             srclen = 0;
  195.             usage();
  196.             exit(1);
  197.             }
  198.         }
  199.         }
  200.     } else if (version == 2 && dstlen == 0 && !trivialSNMPv2){
  201.         dstlen = MAX_NAME_LEN;
  202.         party_scanInit();
  203.         for(pp = party_scanNext(); pp; pp = party_scanNext()){
  204.         if (!strcasecmp(pp->partyName, argv[arg])){
  205.             dstlen = pp->partyIdentityLen;
  206. #ifdef SVR4
  207.             memmove(dst, pp->partyIdentity, dstlen * sizeof(oid));
  208. #else
  209.             bcopy(pp->partyIdentity, dst, dstlen * sizeof(oid));
  210. #endif
  211.             break;
  212.         }
  213.         }
  214.         if (!pp){
  215.         if (!read_objid(argv[arg], dst, &dstlen)){
  216.             printf("Invalid destination party: %s\n", argv[arg]);
  217.             dstlen = 0;
  218.             usage();
  219.             exit(1);
  220.         }
  221.         }
  222.         } else if (version == 2 && contextlen == 0 && !trivialSNMPv2){
  223.             contextlen = MAX_NAME_LEN;
  224.             context_scanInit();
  225.             for(cxp = context_scanNext(); cxp; cxp = context_scanNext()){
  226.                 if (!strcasecmp(cxp->contextName, argv[arg])){
  227.                     contextlen = cxp->contextIdentityLen;
  228. #ifdef SVR4
  229.                     memmove(context, cxp->contextIdentity,
  230.                           contextlen * sizeof(oid));
  231. #else
  232.                     bcopy(cxp->contextIdentity, context,
  233.                           contextlen * sizeof(oid));
  234. #endif
  235.                     break;
  236.                 }
  237.             }
  238.             if (!cxp){
  239.                 if (!read_objid(argv[arg], context, &contextlen)){
  240.                     printf("Invalid context: %s\n", argv[arg]);
  241.                     contextlen = 0;
  242.             usage();
  243.                     exit(1);
  244.                 }
  245.             }
  246.     } else {
  247.         names[current_name++] = argv[arg++];
  248.         if (arg < argc)
  249.         switch(*argv[arg]){
  250.             case 'i':
  251.                 case 's':
  252.                 case 'x':
  253.                 case 'd':
  254.                 case 'n':
  255.                 case 'o':
  256.                 case 't':
  257.                 case 'a':
  258.                 types[current_type++] = *argv[arg++];
  259.             break;
  260.             default:
  261.             printf("Bad object type: %c\n", *argv[arg]);
  262.             usage();
  263.             exit(1);
  264.         }
  265.         if (arg < argc)
  266.             values[current_value++] = argv[arg];
  267.     }
  268.     }
  269.  
  270.     if (!hostname || current_name <= 0 || (version < 1) || (version > 2)
  271.     || current_name != current_type    || current_type != current_value
  272.     || (version == 1 && !community)
  273.     || (version == 2 && (!srclen || !dstlen || !contextlen)
  274.         && !trivialSNMPv2)){
  275.     usage();
  276.     exit(1);
  277.     }
  278.  
  279.     if (trivialSNMPv2){
  280.     if ((destAddr = inet_addr(hostname)) == -1){
  281.         hp = gethostbyname(hostname);
  282.         if (hp == NULL){
  283.         fprintf(stderr, "unknown host: %s\n", hostname);
  284.         exit(1);
  285.         } else {
  286. #ifdef SVR4
  287.         memmove((char *)&destAddr, (char *)hp->h_addr,
  288.               hp->h_length);
  289. #else
  290.         bcopy((char *)hp->h_addr, (char *)&destAddr,
  291.               hp->h_length);
  292. #endif
  293.         }
  294.     }
  295.     srclen = dstlen = contextlen = MAX_NAME_LEN;
  296.     ms_party_init(destAddr, src, &srclen, dst, &dstlen,
  297.               context, &contextlen);
  298.     }
  299.  
  300.     if (clock_flag){
  301.         pp = party_getEntry(src, srclen);
  302.         if (pp){
  303.             pp->partyAuthClock = srcclock;
  304.             gettimeofday(&pp->tv, (struct timezone *)0);
  305.             pp->tv.tv_sec -= pp->partyAuthClock;
  306.         }
  307.         pp = party_getEntry(dst, dstlen);
  308.         if (pp){
  309.             pp->partyAuthClock = dstclock;
  310.             gettimeofday(&pp->tv, (struct timezone *)0);
  311.             pp->tv.tv_sec -= pp->partyAuthClock;
  312.         }
  313.     }
  314.  
  315. #ifdef SVR4
  316.     memset((char *)&session, NULL, sizeof(struct snmp_session));
  317. #else
  318.     bzero((char *)&session, sizeof(struct snmp_session));
  319. #endif
  320.     session.peername = hostname;
  321.     if (port_flag)
  322.         session.remote_port = dest_port;
  323.  
  324.     if (version == 1){
  325.         session.version = SNMP_VERSION_1;
  326.         session.community = (u_char *)community;
  327.         session.community_len = strlen((char *)community);
  328.     } else if (version == 2){
  329.         session.version = SNMP_VERSION_2;
  330.         session.srcParty = src;
  331.         session.srcPartyLen = srclen;
  332.         session.dstParty = dst;
  333.         session.dstPartyLen = dstlen;
  334.         session.context = context;
  335.         session.contextLen = contextlen;
  336.     }
  337.     if (retransmission_flag)
  338.         session.retries = retransmission;
  339.     else
  340.         session.retries = SNMP_DEFAULT_RETRIES;
  341.     if (timeout_flag)
  342.         session.timeout = timeout;
  343.     else
  344.         session.timeout = SNMP_DEFAULT_TIMEOUT;
  345.  
  346.     session.authenticator = NULL;
  347.     snmp_synch_setup(&session);
  348.     ss = snmp_open(&session);
  349.     if (ss == NULL){
  350.     printf("Couldn't open snmp\n");
  351.     exit(-1);
  352.     }
  353.  
  354.     pdu = snmp_pdu_create(SET_REQ_MSG);
  355.  
  356.     for(count = 0; count < current_name; count++){
  357.     name_length = MAX_NAME_LEN;
  358.     if (!read_objid(names[count], name, &name_length)){
  359.         printf("Invalid object identifier: %s\n", names[count]);
  360.     }
  361.     
  362.     snmp_add_var(pdu, name, name_length, types[count], values[count]);
  363.     }
  364.  
  365. retry:
  366.     status = snmp_synch_response(ss, pdu, &response);
  367.     if (status == STAT_SUCCESS){
  368.     if (response->errstat == SNMP_ERR_NOERROR){
  369.         for(vars = response->variables; vars; vars = vars->next_variable)
  370.         print_variable(vars->name, vars->name_length, vars);
  371.     } else {
  372.         printf("Error in packet.\nReason: %s\n", snmp_errstring(response->errstat));
  373.         if (response->errstat == SNMP_ERR_NOSUCHNAME){
  374.         printf("This name doesn't exist: ");
  375.         for(count = 1, vars = response->variables; vars && count != response->errindex;
  376.             vars = vars->next_variable, count++)
  377.             ;
  378.         if (vars)
  379.             print_objid(vars->name, vars->name_length);
  380.         printf("\n");
  381.         }
  382.         if ((pdu = snmp_fix_pdu(response, SET_REQ_MSG)) != NULL)
  383.         goto retry;
  384.     }
  385.  
  386.     } else if (status == STAT_TIMEOUT){
  387.     printf("No Response from %s\n", hostname);
  388.     } else {    /* status == STAT_ERROR */
  389.     printf("An error occurred, Quitting\n");
  390.     }
  391.  
  392.     if (response)
  393.     snmp_free_pdu(response);
  394.     snmp_close(ss);
  395.  
  396.     return 0;
  397. }
  398.  
  399. /*
  400.  * Add a variable with the requested name to the end of the list of
  401.  * variables for this pdu.
  402.  */
  403. void
  404. snmp_add_var(pdu, name, name_length, type, value)
  405.     struct snmp_pdu *pdu;
  406.     oid *name;
  407.     int name_length;
  408.     char type, *value;
  409. {
  410.     struct variable_list *vars;
  411.     char buf[2048];
  412.  
  413.     if (pdu->variables == NULL){
  414.     pdu->variables = vars =
  415.         (struct variable_list *)malloc(sizeof(struct variable_list));
  416.     } else {
  417.     for(vars = pdu->variables;
  418.         vars->next_variable;
  419.         vars = vars->next_variable)
  420.         /*EXIT*/;
  421.     vars->next_variable =
  422.         (struct variable_list *)malloc(sizeof(struct variable_list));
  423.     vars = vars->next_variable;
  424.     }
  425.  
  426.     vars->next_variable = NULL;
  427.     vars->name = (oid *)malloc(name_length * sizeof(oid));
  428. #ifdef SVR4
  429.     memmove((char *)vars->name, (char *)name, name_length * sizeof(oid));
  430. #else
  431.     bcopy((char *)name, (char *)vars->name, name_length * sizeof(oid));
  432. #endif
  433.     vars->name_length = name_length;
  434.  
  435.     switch(type){
  436.     case 'i':
  437.         vars->type = INTEGER;
  438.         vars->val.integer = (long *)malloc(sizeof(long));
  439.         *(vars->val.integer) = atoi(value);
  440.         vars->val_len = sizeof(long);
  441.         break;
  442.     case 's':
  443.     case 'x':
  444.     case 'd':
  445.         vars->type = STRING;
  446.         if (type == 'd'){
  447.         vars->val_len = ascii_to_binary((u_char *)value, buf);
  448.         } else if (type == 's'){
  449.         strcpy(buf, value);
  450.         vars->val_len = strlen(buf);
  451.         } else if (type == 'x'){
  452.         vars->val_len = hex_to_binary((u_char *)value, buf);
  453.         }
  454.         vars->val.string = (u_char *)malloc(vars->val_len);
  455. #ifdef SVR4
  456.         memmove((char *)vars->val.string, (char *)buf, vars->val_len);
  457. #else
  458.         bcopy((char *)buf, (char *)vars->val.string, vars->val_len);
  459. #endif
  460.         break;
  461.     case 'n':
  462.         vars->type = NULLOBJ;
  463.         vars->val_len = 0;
  464.         vars->val.string = NULL;
  465.         break;
  466.     case 'o':
  467.         vars->type = OBJID;
  468.         vars->val_len = MAX_NAME_LEN;
  469.         read_objid(value, (oid *)buf, &vars->val_len);
  470.         vars->val_len *= sizeof(oid);
  471.         vars->val.objid = (oid *)malloc(vars->val_len);
  472. #ifdef SVR4
  473.         memmove((char *)vars->val.objid, (char *)buf, vars->val_len);
  474. #else
  475.         bcopy((char *)buf, (char *)vars->val.objid, vars->val_len);
  476. #endif
  477.         break;
  478.     case 't':
  479.         vars->type = TIMETICKS;
  480.         vars->val.integer = (long *)malloc(sizeof(long));
  481.         *(vars->val.integer) = atoi(value);
  482.         vars->val_len = sizeof(long);
  483.         break;
  484.     case 'a':
  485.         vars->type = IPADDRESS;
  486.         vars->val.integer = (long *)malloc(sizeof(long));
  487.         *(vars->val.integer) = inet_addr(value);
  488.         vars->val_len = sizeof(long);
  489.         break;
  490.     default:
  491.         printf("Internal error in type switching\n");
  492.         exit(-1);
  493.     }
  494. }
  495.  
  496. static int
  497. ascii_to_binary(cp, bufp)
  498.     u_char  *cp;
  499.     u_char *bufp;
  500. {
  501.     int    subidentifier;
  502.     u_char *bp = bufp;
  503.  
  504.     for(; *cp != '\0'; cp++){
  505.     if (isspace(*cp))
  506.         continue;
  507.     if (!isdigit(*cp)){
  508.         fprintf(stderr, "Input error\n");
  509.         return -1;
  510.     }
  511.     subidentifier = atoi(cp);
  512.     if (subidentifier > 255){
  513.         fprintf(stderr, "subidentifier %d is too large ( > 255)\n",
  514.             subidentifier);
  515.         return -1;
  516.     }
  517.     *bp++ = (u_char)subidentifier;
  518.     while(isdigit(*cp))
  519.         cp++;
  520.     cp--;
  521.     }
  522.     return bp - bufp;
  523. }
  524.  
  525. static int
  526. hex_to_binary(cp, bufp)
  527.     u_char  *cp;
  528.     u_char *bufp;
  529. {
  530.     int    subidentifier;
  531.     u_char *bp = bufp;
  532.  
  533.     for(; *cp != '\0'; cp++){
  534.     if (isspace(*cp))
  535.         continue;
  536.     if (!isxdigit(*cp)){
  537.         fprintf(stderr, "Input error\n");
  538.         return -1;
  539.     }
  540.     sscanf((char *)cp, "%x", &subidentifier);
  541.     if (subidentifier > 255){
  542.         fprintf(stderr, "subidentifier %d is too large ( > 255)\n",
  543.             subidentifier);
  544.         return -1;
  545.     }
  546.     *bp++ = (u_char)subidentifier;
  547.     while(isxdigit(*cp))
  548.         cp++;
  549.     cp--;
  550.     }
  551.     return bp - bufp;
  552. }
  553.